package com.yuexunit.mobilenurse.module.BloodManage.presenter.impl;

import com.yuexunit.mobilenurse.config.AppConfig;
import com.yuexunit.mobilenurse.module.BloodManage.bean.CheckDataBean;
import com.yuexunit.mobilenurse.module.BloodManage.model.IReceiveModel;
import com.yuexunit.mobilenurse.module.BloodManage.presenter.IReceivePresenter;
import com.yuexunit.mobilenurse.module.BloodManage.ui.view.IReceiveView;
import com.yuexunit.mobilenurse.util.ProUtil;

import org.kymjs.kjframe.ui.ViewInject;
import org.slf4j.Logger;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by work-jx on 2017/10/25.
 */
public class ReceivePresenter implements IReceivePresenter{
    private IReceiveView view;
    private IReceiveModel model;

    private final Logger log = ProUtil.getLogger(ReceivePresenter.class);
    public CompositeSubscription compositeSubscription = new CompositeSubscription();

    public ReceivePresenter(IReceiveModel model, IReceiveView view) {
        this.model = model;
        this.view = view;
    }

    @Override
    public void receiveBlood(Map<String, String> praise) {
        model.receiveBlood(praise)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        log.info("UserPresenter_doInLogin_SHOW_DIALOG");
                        view.loadingDialogStatus(AppConfig.SHOW_DIALOG);
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<CheckDataBean>() {
                               @Override
                               public void onCompleted() {
                                   view.loadingDialogStatus(AppConfig.DISMISS_DIALOG);
                               }

                               @Override
                               public void onError(Throwable e) {
                                   ViewInject.toast("接受失败");
                               }

                               @Override
                               public void onNext(CheckDataBean checkDataBean) {
                                   view.receiveBlood(checkDataBean);
                               }
                           }
                );
    }
}
